﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardInChina.Data.Entity.Interface;
using CardInChina.Data.Entity.Entity;
using CardInChina.Bussiness.Frontend.Models;

namespace CardInChina.Bussiness.Frontend.Interface
{
    public class GameBusinessService
    {
        private static GameService gs = new GameService();
        //private static string[] index_str = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z" };
        private static string[] index_str = Enumerable.Range((int)'A', 26).Select(p => ((char)p).ToString()).ToArray();


        private Models.Game ConvertToGameFromGame(CardInChina.Data.Entity.Entity.Game game)
        {
            if (game == null) return null;
            Models.Game g = new Models.Game();
            g.GameID = game.GameID;
            g.GameName = game.GameName;
            g.IsHot = game.IsHot;
            g.GameKey = game.GameKey;
            g.GamePhoto = game.GamePhoto;
            return g;
        }

        private IEnumerable<Models.Game> ConvertToGamesFromGames(IEnumerable<CardInChina.Data.Entity.Entity.Game> games)
        {
            if (games == null) return null;
            IList<Models.Game> ret = new List<Models.Game>();
            foreach (var g in games)
                ret.Add(ConvertToGameFromGame(g));
            return ret.ToArray();
        }

        private GamesByIndex ConvertToGamesByIndexFromGame(IEnumerable<CardInChina.Data.Entity.Entity.Game> games, string index)
        {
            if(string.IsNullOrEmpty(index)) return null;
            GamesByIndex gbi = new GamesByIndex();
            gbi.IndexName = index;
            gbi.Games = ConvertToGamesFromGames(games);
            return gbi;
 
        }

        public IEnumerable<GamesByIndex> GetGamesWithAllIndex()
        {
            List<GamesByIndex> ret = new List<GamesByIndex>();
            foreach (string i in index_str){
                ret.Add(ConvertToGamesByIndexFromGame(gs.GetGamesByIndex(i),i));
            }
            return ret;
        }

        public IEnumerable<Data.Entity.Entity.Game> GetHotGames()
        {
            return gs.GetHotGames();
        }

        public GamesByIndex GetOtherGames()
        {
            return ConvertToGamesByIndexFromGame(gs.GetOtherGames(), "其他游戏");
        }

        public IEnumerable<Data.Entity.Entity.Game> Get10HotTopGames()
        {
            return gs.GetHotTopGames(10);
 
        }

        public IEnumerable<Models.Game> SearchGameByKeyword(string Keyword)
        {
            if (string.IsNullOrEmpty(Keyword)) return ConvertToGamesFromGames(this.Get10HotTopGames());
            return ConvertToGamesFromGames(gs.GetGamesByKeyword(Keyword));
 
        }

        public Models.Game GetGameByGameKey(string gameKey)
        {
            return ConvertToGameFromGame(gs.GetGameByKey(gameKey));
        }
    }
}
